{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Scenarios\n",
    "1. Run 1 algorithm with 1 problem, and multiple trials \n",
    "2. Run 1 algorithm with multiple problems, and multiple trials\n",
    "3. Run multiple algorithms with 1 problem, and multiple trials\n",
    "4. Run multiple algorithms with multiple problems, and multiple trials"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "## Import libraries\n",
    "from opfunu.cec_based.cec2017 import F52017, F102017, F292017\n",
    "from mealpy import FloatVar\n",
    "from mealpy import BBO, DE\n",
    "from mealpy import Multitask        # this is the utility class we will use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "#### Define problems\n",
    "f1 = F52017(30, f_bias=0)\n",
    "f2 = F102017(30, f_bias=0)\n",
    "f3 = F292017(30, f_bias=0)\n",
    "\n",
    "p1 = {\n",
    "    \"bounds\": FloatVar(lb=f1.lb, ub=f1.ub),\n",
    "    \"obj_func\": f1.evaluate,\n",
    "    \"minmax\": \"min\",\n",
    "    \"name\": \"F5\",\n",
    "    \"log_to\": None\n",
    "}\n",
    "\n",
    "p2 = {\n",
    "    \"bounds\": FloatVar(lb=f2.lb, ub=f2.ub),\n",
    "    \"obj_func\": f2.evaluate,\n",
    "    \"minmax\": \"min\",\n",
    "    \"name\": \"F10\",\n",
    "    \"log_to\": None\n",
    "}\n",
    "\n",
    "p3 = {\n",
    "    \"bounds\": FloatVar(lb=f3.lb, ub=f3.ub),\n",
    "    \"obj_func\": f3.evaluate,\n",
    "    \"minmax\": \"min\",\n",
    "    \"name\": \"F29\",\n",
    "    \"log_to\": None\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "## Define models\n",
    "model1 = BBO.DevBBO(epoch=10, pop_size=50)\n",
    "model2 = BBO.OriginalBBO(epoch=10, pop_size=50)\n",
    "model3 = DE.OriginalDE(epoch=10, pop_size=50)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1. Run 1 algorithm with 1 problem, and multiple trials "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solving problem: F5 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F5 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F5 using algorithm: DevBBO, on the: 3 trial\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    multitask = Multitask(algorithms=(model1, ), problems=(p1, ))  # Need the \",\" because it is a tuple >= 1.\n",
    "    multitask.execute(n_trials=3, save_path=\"history1\", save_as=\"csv\", verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 2. Run 1 algorithm with multiple problems, and multiple trials"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solving problem: F5 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F5 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F5 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 3 trial\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    multitask = Multitask(algorithms=(model1, ), problems=(p1, p2, p3), n_workers=4)  # Need the \",\" because it is a tuple >= 1.\n",
    "    multitask.execute(n_trials=3, save_path=\"history2\", save_as=\"csv\", verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 3. Run multiple algorithms with 1 problem, and multiple trials"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solving problem: F29 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 3 trial\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    multitask = Multitask(algorithms=(model1, model2, model3), problems=(p3, ), n_workers=3)  # Need the \",\" because it is a tuple >= 1.\n",
    "    multitask.execute(n_trials=3, save_path=\"history3\", save_as=\"csv\", verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 4. Run multiple algorithms with multiple problems, and multiple trials"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solving problem: F5 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F5 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F5 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F5 using algorithm: OriginalBBO, on the: 1 trial\n",
      "Solving problem: F5 using algorithm: OriginalBBO, on the: 2 trial\n",
      "Solving problem: F5 using algorithm: OriginalBBO, on the: 3 trial\n",
      "Solving problem: F10 using algorithm: OriginalBBO, on the: 1 trial\n",
      "Solving problem: F10 using algorithm: OriginalBBO, on the: 2 trial\n",
      "Solving problem: F10 using algorithm: OriginalBBO, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 3 trial\n",
      "Solving problem: F5 using algorithm: OriginalDE, on the: 1 trial\n",
      "Solving problem: F5 using algorithm: OriginalDE, on the: 2 trial\n",
      "Solving problem: F5 using algorithm: OriginalDE, on the: 3 trial\n",
      "Solving problem: F10 using algorithm: OriginalDE, on the: 1 trial\n",
      "Solving problem: F10 using algorithm: OriginalDE, on the: 2 trial\n",
      "Solving problem: F10 using algorithm: OriginalDE, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 3 trial\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    multitask = Multitask(algorithms=(model1, model2, model3), problems=(p1, p2, p3), n_workers=4) \n",
    "    multitask.execute(n_trials=3, save_path=\"history4\", save_as=\"csv\", verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 5. Faster training with parallelization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solving problem: F5 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F5 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F5 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F5 using algorithm: OriginalBBO, on the: 1 trial\n",
      "Solving problem: F5 using algorithm: OriginalBBO, on the: 2 trial\n",
      "Solving problem: F5 using algorithm: OriginalBBO, on the: 3 trial\n",
      "Solving problem: F10 using algorithm: OriginalBBO, on the: 1 trial\n",
      "Solving problem: F10 using algorithm: OriginalBBO, on the: 2 trial\n",
      "Solving problem: F10 using algorithm: OriginalBBO, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 3 trial\n",
      "Solving problem: F5 using algorithm: OriginalDE, on the: 1 trial\n",
      "Solving problem: F5 using algorithm: OriginalDE, on the: 2 trial\n",
      "Solving problem: F5 using algorithm: OriginalDE, on the: 3 trial\n",
      "Solving problem: F10 using algorithm: OriginalDE, on the: 1 trial\n",
      "Solving problem: F10 using algorithm: OriginalDE, on the: 2 trial\n",
      "Solving problem: F10 using algorithm: OriginalDE, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 3 trial\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    multitask = Multitask(algorithms=(model1, model2, model3), problems=(p1, p2, p3), mode=(\"thread\", ), n_workers=4) \n",
    "    multitask.execute(n_trials=3, n_jobs=4, save_path=\"history5\", save_as=\"csv\", verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 6. Saving convergence data (The best fitness in each epoch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solving problem: F5 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F5 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F5 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F10 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: DevBBO, on the: 3 trial\n",
      "Solving problem: F5 using algorithm: OriginalBBO, on the: 1 trial\n",
      "Solving problem: F5 using algorithm: OriginalBBO, on the: 2 trial\n",
      "Solving problem: F5 using algorithm: OriginalBBO, on the: 3 trial\n",
      "Solving problem: F10 using algorithm: OriginalBBO, on the: 1 trial\n",
      "Solving problem: F10 using algorithm: OriginalBBO, on the: 2 trial\n",
      "Solving problem: F10 using algorithm: OriginalBBO, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: OriginalBBO, on the: 3 trial\n",
      "Solving problem: F5 using algorithm: OriginalDE, on the: 1 trial\n",
      "Solving problem: F5 using algorithm: OriginalDE, on the: 2 trial\n",
      "Solving problem: F5 using algorithm: OriginalDE, on the: 3 trial\n",
      "Solving problem: F10 using algorithm: OriginalDE, on the: 1 trial\n",
      "Solving problem: F10 using algorithm: OriginalDE, on the: 2 trial\n",
      "Solving problem: F10 using algorithm: OriginalDE, on the: 3 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 1 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 2 trial\n",
      "Solving problem: F29 using algorithm: OriginalDE, on the: 3 trial\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    multitask = Multitask(algorithms=(model1, model2, model3), problems=(p1, p2, p3), modes=(\"thread\",), n_workers=5) \n",
    "    multitask.execute(n_trials=3, save_path=\"history6\", save_as=\"csv\", save_convergence=True, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}